package sk.xorty.service;

import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Service;
import sk.xorty.domain.Message;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MessageBoardServiceImpl implements MessageBoardService {

    private Map<Long, Message> messages = new HashMap<Long, Message>();

    @Secured({"ROLE_USER", "ROLE_GUEST"})
    @Override
    public List<Message> listMessages() {
        return new ArrayList<Message>(messages.values());
    }

    @Secured("ROLE_USER")
    @Override
    public synchronized void postMessage(Message message) {
        message.setId(System.currentTimeMillis());
        messages.put(message.getId(), message);
    }

    @Secured("ROLE_ADMIN")
    @Override
    public synchronized void deleteMessage(Message message) {
        messages.remove(message.getId());
    }

    @Secured({"ROLE_USER", "ROLE_GUEST"})
    @Override
    public Message findById(Long id) {
        return messages.get(id);
    }
}
